package com.belarus.kamianiec.photo.photoSite.web.ctrl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.belarus.kamianiec.photo.photoSite.domain.News;
import com.belarus.kamianiec.photo.photoSite.service.NewsService;

//@Controller
public class NewsController {

	private NewsService newsService;

	@RequestMapping("/news")
	public String listNews(Map<String, Object> map) {
		map.put("news", new News());
		map.put("newsList", newsService.listNews());
		return "news";
	}

	@RequestMapping(value = "/newsadd", method = RequestMethod.POST)
	public String addNews(@ModelAttribute("news") News news, BindingResult result) {
		newsService.addNews(news);
		return "redirect:/news";
	}

	@RequestMapping("/newsdel/{newsId}")
	public String deleteNews(@PathVariable("newsId") Integer newsId) {
		newsService.removeNews(newsId);
		return "redirect:/news";
	}

	public NewsService getNewsService() {
		return newsService;
	}

	@Autowired
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
}
